package com.wgj.android.web;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.LinearLayout;

import com.google.gson.Gson;
import com.jiaai.xui.base.XActivity;
import com.jiaai.xui.base.XFragment;
import com.jiaai.xui.jsbridge.BridgeHandler;
import com.jiaai.xui.jsbridge.BridgeWebView;
import com.jiaai.xui.jsbridge.CallBackFunction;
import com.jiaai.xui.utils.XDensityUtils;
import com.jiaai.xui.utils.log.XLog;
import com.jiaai.xui.view.widget.XTopbarView;
import com.jiaai.xui.view.widget.loadingview.XLoadingView;
import com.wgj.android.R;
import com.wgj.android.web.entity.WebConfig;

import org.json.JSONObject;

/**
 * Created by Administrator on 2017/10/27.
 */

public class WGJWebFragment extends XFragment {
    public  static  final  String WEB_CONFIG="web_config";
    private BridgeWebView webView;
    private XTopbarView topbarView;
    private LinearLayout webviewWarp;
    private  WebConfig config;

    public static WGJWebFragment getInstance(WebConfig config){
         WGJWebFragment wgjWebFragment=new WGJWebFragment();
         Bundle bundle=new Bundle();
         bundle.putSerializable(WEB_CONFIG,config);
         wgjWebFragment.setArguments(bundle);
         return  wgjWebFragment;
    }

    @Override
    public int getLayoutId() {
        return R.layout.wgj_web_activity;
    }

    @Override
    public void initData(Bundle bundle) {
      config= (WebConfig) getArguments().getSerializable(WEB_CONFIG);
    }

    @Override
    public void initView() {
        topbarView=getView().findViewById(R.id.topBar);
        webviewWarp= getView().findViewById(R.id.webviewWarp);

        webView= new BridgeWebView(getActivity().getApplicationContext());
        webView.setBridgeWebViewClient(new WGJWebClient(webView));
        if(config.getShowTopBar()==1){
            topbarView.setVisibility(View.VISIBLE);
            topbarView.setTitle(config.getTitle());
        }else{
            topbarView.setVisibility(View.GONE);
        }
        webView.getSettings().setTextSize(WebSettings.TextSize.SMALLER);

        webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
                topbarView.setTitle(title);
            }
        });
        webView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT));
        webviewWarp.addView(webView);
        WGJWebFunctionUtils.bindFunction(webView,getFragmentManager());
        WGJWebFunctionUtils.initSetting(webView);
        webView.loadUrl(config.getUrl());

    }


}
